import requests
from lxml import etree
import htmlmin

base_url = "https://www.medchemexpress.cn"


def search_procode(procode):
    """搜索货号信息
    """
    headers = {
        "Accept":
        "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "Accept-Language":
        "zh-CN,zh;q=0.9",
        "Connection":
        "keep-alive",
        "Referer":
        "https://www.medchemexpress.cn/",
        "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
    }
    url = "https://www.medchemexpress.cn/search.html"
    params = {
        "q": procode,
    }
    response = requests.get(url, headers=headers, params=params)
    html = etree.HTML(response.text)
    text = html.xpath('//*[@id="main"]/div[7]/div[1]/ul/li[1]/dl/dt/a//text()')
    result = ''.join(text).strip()
    print(f"搜索结果:{result}")
    if result == procode:
        page = html.xpath('//*[@id="main"]/div[7]/div[1]/ul/li[1]/dl/dt/a/@href')[0]
        page_url = base_url + page
        print(page_url)
        return page_url
    else:
        return None


def get_page_html(url):
    """获取页面xpath
    """
    headers = {
        "Accept":
        "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "Accept-Language":
        "zh-CN,zh;q=0.9",
        "Connection":
        "keep-alive",
        "Referer":
        "https://www.medchemexpress.cn/",
        "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
    }
    response = requests.get(url, headers=headers)
    html = etree.HTML(response.text)
    return html


def get_spu_list(html):
    """获取spu列表
    """
    text = html.xpath('//*[@class="price_tbl"]/tr[position()>1]')
    spu_list = []
    for item in text:
        spec_list = item.xpath('./td[1]//text()')
        price_list = item.xpath('./td[2]//text()')
        spec_res = ''.join(spec_list).strip().replace(' ', '')
        price_res = ''.join(price_list).strip().replace('￥', '')
        spu_dict = {'spec': spec_res, 'price': price_res}

        spu_list.append(spu_dict)
    return spu_list


def get_prodes(html):
    """获取产品描述
    """
    tr_list = html.xpath('//*[@class="details_info_tbl"]/tr[position()<11]')
    banner_html = """
    <div id="con_header" class="div" style="display: flex;">
        <a href="https://gzcxsw.top/#/user/gift" title="点击进入积分商城" target="_blank">
            <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/b8a6ee5cd5e9b4ceb809b9f47898dbe.png" height="120" width="400">
        </a>
        <a href="https://work.weixin.qq.com/kfid/kfcd1ffef164b328abc" title="点击在线客服咨询" target="_blank">
            <img src="https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/d319a18f235ab79d6133e8b3d0382fa.png" height="120" width="400">
        </a>
    </div>
    """
    temp_des = ""
    for item in tr_list:
        # temp_des += etree.tostring(item, pretty_print=False).decode('utf-8')
        temp_des += etree.tostring(item, pretty_print=False).decode('utf-8')
    return banner_html + htmlmin.minify(temp_des)


def procode_process(procode):
    """
    处理货号(删除最后一位规格)
    """
    temp_str = procode.split("-")
    return temp_str[0] + '-' + temp_str[1]


if __name__ == "__main__":
    print("Hello World")
